<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-label">
        <button class="login-btn" @click="submitLogin">登录</button>
      </div>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="90px" class="login-form">
        <h1 class="title">数学建模校赛网站</h1>
        <el-form-item label="账号" prop="username" required>
          <el-input v-model="form.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password" required>
          <el-input v-model="form.password" placeholder="请输入密码" type="password" />
        </el-form-item>
        <el-form-item label="角色">
          <el-radio-group v-model="form.role">
            <el-radio label="student">学生</el-radio>
            <el-radio label="teacher">教师</el-radio>
          </el-radio-group>
        </el-form-item>
        <div v-if="error" class="error" style="color:#d32f2f; font-size:18px; margin-top:10px;">{{ error }}</div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
// import { ElMessage } from 'element-plus' // 已不使用，移除

const router = useRouter()
const formRef = ref(null)
const error = ref('')
const form = ref({
  username: '',
  password: '',
  role: 'student'
})
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const submitLogin = () => {
  error.value = ''
  formRef.value.validate((valid) => {
    if (!valid) return
    if (form.value.username === 'admin') {
      error.value = '请前往管理员专用登录入口'
      return
    }
    localStorage.setItem('username', form.value.username)
    localStorage.setItem('userRole', form.value.role)
    if (form.value.role === 'student') {
      router.push('/student/download-topic')
    } else if (form.value.role === 'teacher') {
      router.push('/teacher/preview-score')
    }
  })
}
</script>

<script>
export default {
  name: 'PublicLogin'
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e0e7ff 0%, #f8fafc 100%);
}
.title {
  margin-bottom: 40px;
  font-size: 48px;
  font-family: 'KaiTi', 'STKaiti', serif;
  text-align: center;
  color: #2d3a4b;
  letter-spacing: 6px;
  font-weight: bold;
  white-space: nowrap;
  line-height: 1.2;
  width: 100%;
  display: block;
  margin-left: auto;
  margin-right: auto;
}
.login-box {
  border: none;
  padding: 60px 60px 50px 200px;
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 18px;
  position: relative;
  min-width: 600px;
  box-shadow: 0 8px 32px 0 rgba(60, 90, 150, 0.15);
}
.login-label {
  position: absolute;
  left: 30px;
  top: 50%;
  transform: translateY(-50%);
  width: 100px;
  height: 180px;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  font-family: 'KaiTi', 'STKaiti', serif;
  letter-spacing: 8px;
  background: #f1f5fa;
  border-radius: 12px;
  color: #3b5998;
  box-shadow: 0 2px 12px 0 rgba(60, 90, 150, 0.08);
}
.login-form {
  display: flex;
  flex-direction: column;
  gap: 32px;
  margin-left: 80px;
  width: 420px;
}
.form-row {
  display: flex;
  align-items: center;
  font-size: 24px;
  margin-bottom: 0;
  justify-content: flex-start;
}
.form-row label {
  width: 90px;
  text-align: left;
  margin-right: 12px;
  font-family: 'KaiTi', 'STKaiti', serif;
  color: #2d3a4b;
  flex-shrink: 0;
  letter-spacing: 2px;
}
.form-row input[type="text"],
.form-row input[type="password"] {
  width: 300px;
  height: 54px;
  font-size: 22px;
  padding: 6px 16px;
  border: 2px solid #3b5998;
  border-radius: 8px;
  font-family: 'KaiTi', 'STKaiti', serif;
  background: #f9f9f9;
  transition: border 0.2s, box-shadow 0.2s;
  box-sizing: border-box;
  outline: none;
  margin-left: 0;
  /* 统一内外边距，防止聚焦时尺寸变化 */
  box-shadow: none;
}
.form-row input[type="text"]:focus,
.form-row input[type="password"]:focus {
  border: 2px solid #2d3a4b;
  background: #fff;
  /* box-shadow 只做内阴影，不影响外部尺寸 */
  box-shadow: 0 0 0 2px #e0e7ff inset;
}
.role-row {
  justify-content: flex-start;
}
.role-row label {
  width: auto;
  margin-right: 10px;
  font-size: 22px;
  text-align: left;
}
.role-row input[type="radio"] {
  margin: 0 8px 0 20px;
  width: 20px;
  height: 20px;
}
.role-row label[for="student"] {
  margin-left: 0;
}
.register-tip {
  margin-top: 10px;
  font-size: 22px;
  text-align: left;
  font-family: 'KaiTi', 'STKaiti', serif;
  color: #222;
}
.register-tip a {
  color: #3b5998;
  text-decoration: underline;
  cursor: pointer;
  margin: 0 2px;
}
/* 登录按钮与表单左对齐 */
.login-label {
  left: 30px;
}
.login-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 90px;
  width: 120px;
  height: 120px;
  font-size: 40px;
  font-family: 'KaiTi', 'STKaiti', serif;
  letter-spacing: 8px;
  background: linear-gradient(135deg, #9cb8f6 0%, #5b7fff 100%);
  color: #fff;
  border: none;
  border-radius: 18px;
  box-shadow: 0 4px 24px 0 rgba(60, 90, 150, 0.18);
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  font-weight: bold;
  text-shadow: 0 2px 8px rgba(60,90,150,0.18), 0 1px 0 #fff;
}
.login-btn:hover {
  background: linear-gradient(135deg, #cad5e3 0%, #6c98f5 100%);
  box-shadow: 0 8px 32px 0 rgba(60, 90, 150, 0.22);
}
</style>